<?php
	error_reporting(E_ALL);
	require_once('../libs/phpThumb/phpthumb.class.php');
	list($a, $file) = preg_split("/\//", $_SERVER['REQUEST_URI'], 2, PREG_SPLIT_NO_EMPTY);
 
	file_mkdir(dirname($file));
	$dir	= preg_split("/\//", $file, -1, PREG_SPLIT_NO_EMPTY);
	$path	= array();
	$option = array();
	while($dir[0]) {
		switch($dir[0]) {
			case is_numeric($dir[0]) && !isset($option['w']): $option['w'] = array_shift($dir); break;
			case is_numeric($dir[0]) && !isset($option['h']): $option['h'] = array_shift($dir); break;
			case $dir[0]=='zc': $option['zc'] = 1; array_shift($dir); break;
			default: $temp[] = array_shift($dir);
		}
	}
	$path = implode('/', $temp);
	$phpThumb = new phpThumb();
	//$phpThumb->config_imagemagick_path = '/usr/local/bin/convert';
	$phpThumb->setSourceFilename('../'.$path);
	$phpThumb->setParameter('q', 100);
	foreach($option as $k=>$v){
		$phpThumb->setParameter($k, $v);
	}
	foreach($_GET as $k=>$v){
		$phpThumb->setParameter($k, $v);
	}
	
	if ($phpThumb->GenerateThumbnail()) {
	 	$phpThumb->RenderToFile($file);
	 	header("location: /thumb/$file");
		exit;
	}

	header("location: error.gif");

	function file_mkdir($name) {
		if(file_exists($name)) return $name;
		mkdir($name, 0755, true);
		return $name;
	}
?>